package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/email?charset=utf8")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()
	//insert
	//var result sql.Result
	//result, err = db.Exec("insert into sequence(id,`key`,current_value) value(?,?,?)", "1233", "t2", "1")
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//lastId, _ := result.LastInsertId()
	//rowAffected, _ := result.RowsAffected()//影响的行
	//fmt.Println("lastId:", lastId)
	//fmt.Println("RowsAffected:", rowAffected)

	var row *sql.Row
	row = db.QueryRow("select * from sequence")
	var id, key, current_value string
	err = row.Scan(&id, &key, &current_value)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(id, "\t", key, "\t", current_value)
	var rows *sql.Rows
	rows, err = db.Query("select * from sequence")
	if err != nil {
		fmt.Println(err)
		return
	}
	/*for rows.Next() {
		var id, key, current_value string
		rows.Scan(&id, &key, &current_value)
		fmt.Println(id, "\t", key, "\t", current_value)
	}*/
	if rows.Next() {
		fmt.Println(rows.Columns())
		var id, key, current_value interface{}//interface里面存的是[]byte字节
		rows.Scan(&id, &key, &current_value)
		fmt.Println("id:", id, string(id.([]byte)))
		fmt.Println("key:", key, string(key.([]byte)))
	}
	rows.Close()//必须释放
	//db.Exec("truncate table sequence")//清空表
}